/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const valid_map = ['admin', 'editor']
  return valid_map.indexOf(str.trim()) >= 0
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url) {
  const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str) {
  const reg = /^[a-z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str) {
  const reg = /^[A-Z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str) {
  const reg = /^[A-Za-z]+$/
  return reg.test(str)
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email) {
  const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return reg.test(email)
}
export function isvalidPhone(str) {
  const reg = /^1[3|4|5|7|8|9][0-9]\d{8}$/
  return reg.test(str)
}

/**
 * 手机号
 * @param str
 * @returns {boolean}
 */
export function isValidTel() {
  let isPer = (rule, value, callback) => {
    if (!value){
       callback(new Error('请输入电话号码'))
    }else  if (!isvalidPhone(value)){
       callback(new Error('请输入正确的11位手机号码'))
    }else {
       callback()
    }
  }
  return isPer;
}
//判断数字
export function isPercentNum() {
  let patter = /((^[1-9]\d*)|^0)$/
  let isPercent = (rule, value, callback) => {
    if (!patter.test(value)) {
      return callback(new Error('只能输入数字'))
    } else {
      callback()
    }
  }
  return isPercent
}
//判断是否是小数---保留一位小数
export function isFloatValue() {
  let patter = /((^[1-9]\d*)|^0)(\.\d{0,1}){0,1}$/
  let isPercent = (rule, value, callback) => {
    if (!patter.test(value)) {
      return callback(new Error('必须非负整数或至多保留一位小数'))
    } else {
      callback()
    }
  }
  return isPercent
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str) {
  if (typeof str === 'string' || str instanceof String) {
    return true
  }
  return false
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg) {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}

/**
 * 日期加减法
 * @param interval
 * @param number
 * @param date
 * @returns {*}
 * @constructor
 */
export function DateAdd(interval,number,date) {
switch(interval) {
  case   "y "   : {
    date.setFullYear(date.getFullYear() + number);
    return date;
    break;
  }
  case   "q "   : {
    date.setMonth(date.getMonth() + number * 3);
    return date;
    break;
  }
  case   "m "   : {
    date.setMonth(date.getMonth() + number);
    return date;
    break;
  }
  case   "w "   : {
    date.setDate(date.getDate() + number * 7);
    return date;
    break;
  }
  case   "d "   : {
    date.setDate(date.getDate() + number);
    return date;
    break;
  }
  case   "h "   : {
    date.setHours(date.getHours() + number);
    return date;
    break;
  }
  case   "m "   : {
    date.setMinutes(date.getMinutes() + number);
    return date;
    break;
  }
  case   "s "   : {
    date.setSeconds(date.getSeconds() + number);
    return date;
    break;
  }
  default   : {
    date.setDate(d.getDate() + number);
    return date;
    break;
  }
}
}
